//
//  PhotoSerial.m
//  XPhoto
//
//  Created by baboy on 2019/6/18.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "PostSerial.h"
#import "BKHttpRequestManager.h"
#import <BKKit/BKApi.h>
#import "HttpResponse.h"
#import "PostTag.h"
#import "QiniuConf.h"

@implementation PostSerial

+ (id)createPostSerial:(PostSerial*)serial withCallback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    id task = [BKApi request:API_SERIAL_CREATE
                parameters:serial
                  callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                      DLOG(@"%@",error);
                      HttpResponse *response = [HttpResponse responseWithDictionary:json];
                      if (!error && ![response isSuccess]) {
                          error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                      }
                      callback(task, response.data,error);
                  }];
    return task;
}
+ (id)createPostSerial:(PostSerial *)serial withCover:(PhotoAsset *)photoAsset withCallback:(void (^)(id _Nonnull, id _Nullable, NSError * _Nullable))callback{
    [self uploadCover:photoAsset
  photoUploadProgress:nil
             callback:^(id _Nonnull info, id _Nullable param, NSError * _Nullable error) {
        if(param){
            serial.cover = [param valueForKey:@"cover"];
            serial.coverThumb = [param valueForKey:@"coverThumb"];
            [self createPostSerial:serial withCallback:callback];
        }
    }];
    return nil;
}

+ (id)updatePostSerial:(PostSerial*)serial withCallback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    id task = [BKApi request:API_SERIAL_UPDATE
                parameters:serial
                  callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                      DLOG(@"%@",error);
                      HttpResponse *response = [HttpResponse responseWithDictionary:json];
                      if (!error && ![response isSuccess]) {
                          error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                      }
                      callback(task, response.data,error);
                  }];
    return task;
}
+ (id)search:(PostSerialSearchArgs *)args withCallback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    id task = [BKApi request:API_SERIAL_SEARCH
                parameters:args
                  callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
                      HttpResponse *response = [HttpResponse responseWithDictionary:json dataItemClass:[PostSerial class]];
                      if (!error && ![response isSuccess]) {
                           error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                      }
                      DLOG(@"%@",error);
                      if (callback) {
                          callback(task, response.data,error);
                      }
                  }];
    return task;
}

+ (id)searchTagsWithSerialId:(NSInteger)serialId withCallback:(nullable void (^)(id _Nonnull task, id _Nullable json,  NSError * _Nullable error))callback{
    
    id task = [BKApi request:API_SERIAL_SEARCH_TAGS
                  parameters:@{@"id":@(serialId)}
                  callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
        
        HttpResponse *response = nil;
        if (!error) {
            response = [HttpResponse responseWithDictionary:json dataItemClass:[PostTag class]];
            if (![response isSuccess]) {
                error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
            }
        }
        
        DLOG(@"%@",error);
        if (callback) {
            callback(task, response.data,error);
        }
    }];
    return task;
}
+ (id)updateCover:(PhotoAsset *)photoAsset
        forSerial:(NSInteger)serialId
photoUploadProgress:(void(^)(NSString *key, float p))progress
         callback:(void (^)(id _Nonnull, id _Nullable, NSError * _Nullable))callback{
    
    [self uploadCover:photoAsset
  photoUploadProgress:progress
             callback:^(id _Nonnull response, id _Nullable param, NSError * _Nullable error) {
        
        PostSerial *updateSerial = [PostSerial new];
        updateSerial.id = serialId;
        updateSerial.cover = [param valueForKey:@"cover"];
        updateSerial.coverThumb = [param valueForKey:@"coverThumb"];
        
        [BKApi request:API_SERIAL_UPDATE_COVER
            parameters:updateSerial
              callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
            
            if (!error && json) {
                HttpResponse *response = [HttpResponse responseWithDictionary:json];
                if (![response isSuccess]) {
                    error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
                }
            }
            callback(task, updateSerial, error);
        }];
    }];
    return nil;
}

+ (id)  uploadCover:(PhotoAsset *)photoAsset
photoUploadProgress:(void(^)(NSString *key, float p))progress
           callback:(void (^)(id _Nonnull, id _Nullable, NSError * _Nullable))callback{
    NSString *path = [NSString stringWithFormat:@"/serial/%ld/cover/%@", [(User*)[User currentUser] uid], [photoAsset.originPath lastPathComponent]];
    [QiniuConf uploadFile:photoAsset.originPath
                 fileName:path
                 progress:progress
               completion:^(QNResponseInfo * _Nonnull info, NSString * _Nonnull key, NSDictionary * _Nonnull resp) {
        
        NSString *cover = [NSString stringWithFormat:@"%@/%@",[QiniuConf defaultConf].domain, key];
        NSString *coverThumb = [NSString stringWithFormat:@"%@-avatarThumb", cover];
        id param = @{
            @"cover":cover,
            @"coverThumb":coverThumb
        };
        NSError *error = nil;
        if (callback) {
            callback(info, param, error);
        }
    }];
    return nil;
}
@end

@implementation PostSerialSearchArgs

@end
